<?php
namespace Common\Model;
use Think\Model;

/**
 * Class PumpWaterModel
 * @package Common\Model
 * 泵站水电表
 */
class PumpWaterModel extends BaseModel{
    public $pix = "";
    public $fieldsStr = '*';
    public $orderStr = 'ps_id desc,we_number asc';

    public function get_list_use_page($map=array(), $cur_page, $rows_num){
        $map = filter_query_map($map);
        if(isset($map['ps_id'])){
            $map['ps_id'][0] = 'eq';
            $map['ps_id'][1] = str_replace('%', '', $map['ps_id'][1]);
        }

        $count = M('PumpWater')->where($map)->count();
        $list = $this->where($map)->order($this->orderStr)->page($cur_page.",$rows_num")->select();
        $station_model = D('PumpingStation');
        foreach($list as $k => $v){
            $sta_temp = $station_model->queryObj($v['ps_id'], 'id');;
            $list[$k]['sta_name'] = $sta_temp['ps_name'];
        }
        return array('total'=>$count, 'list'=>$list);
    }

    public function gen_search($map, $page, $rows_num){
        $station_model = D('PumpingStation');
        $record_model = D('PumpWaterRecord');
        $station_ids = null;
        $status = '';
        $f_id = '';
        if(isset($map['ps_id'])){
            $station_ids = my_array_filter_to_text(str_replace('%', '', $map['ps_id'][1]), ',');
            unset($map['ps_id']);
        }
        if(isset($map['f_id'])){
            $f_id = str_replace('%', '', $map['f_id'][1]);
            $f_id = array_filter(explode(',', $f_id));
            unset($map['f_id']);
        }
        if(isset($map['is_quick_report'])){
            $status = str_replace('%', '', $map['is_quick_report'][1]);
            unset($map['is_quick_report']);
        }
        if($station_ids){
            $stations = $station_model->order('ps_name desc,mobile desc')->select($station_ids);
        }else{
            $stations = $station_model->order('ps_name desc,mobile desc')->select();
        }
        $fys = array();
        //查询出所有设备
        foreach($stations as $station){
            $query_map['ps_id'] = array('eq', $station['id']);
            if($map['we_name']){
                $query_map['we_name'] = $map['we_name'];
            }
            if($map['we_number']){
                $query_map['we_number'] = $map['we_number'];
            }
            $fys = array_merge($fys, $this->where($query_map)->order($this->orderStr)->select());
        }
        $result = array();
        $all_fys = array();
        $error_list = array();
        unset($map);
        foreach($fys as $k => $fy){
            if(!in_array($fy['id'], $f_id)&&!empty($f_id))
                continue;
            $map['eleid'] = array('eq', $fy['id']);
            $map['date'] = array('egt', date('Y-m-d').' 00:00:00');
            $fy_status =  $record_model->where($map)->find();
            if($fy_status)
                $fy_status = 1;
            else
                $fy_status = 0;
            if($status == '' || $fy_status == $status){
                $fy['status'] = $fy_status;
                if($fy_status == 0)
                    $error_list[] = $fy;
                else
                    $all_fys[] = $fy;
            }
        }
        $result = array_merge($error_list, $all_fys);
        $all_fys = array_slice($result, ($page-1)*$rows_num, $rows_num);
        foreach($all_fys as $k => $fy){
            $last_record = $record_model->get_last_one_record($fy['id']);
            $fy_status =  $fy['status'];
            if($status == '' || $fy_status == $status){
                $sta_temp = $station_model->queryObj($fy['ps_id'], 'id');
                $all_fys[$k]['sta_name'] = $sta_temp['ps_name'];
                $all_fys[$k]['work_person'] = D('MobileAdmin')->get_working_names_by_ids($sta_temp['ps_opr']);
                $all_fys[$k]['status_text'] = is_quick_report($fy_status, false);
                $all_fys[$k]['date'] = $last_record['date'];
                $all_fys[$k]['waterdegrees'] = $last_record['waterdegrees'];
                $all_fys[$k]['work_log'] = '<a href="#" onclick="open_easyui_window_with_id(\'log_window\', \''.$sta_temp['ps_name'].'-'.$fy['we_name'].'-'.$fy['we_number'].'\', \'/Manage/AdminPumpWater/just_see?fy_id='.$fy['id'].'\')">查看</a>';
            }
        }
        $footer = array(array('we_name'=>'水表总数', 'we_number'=>count($result)));
        return array('total'=>count($result), 'rows'=>$all_fys, 'footer'=>$footer);
    }

    public function filter_all(){
        $station_model = D('PumpingStation');
        $list = $this->getAll();
        foreach($list as $k => $v){
            $fa = $station_model->queryObj($list[$k]['ps_id'], 'id');
            $list[$k]['ps_id'] = $fa['ps_name'];
        }
        return $list;
    }

    public function query_by_pids($pids,  $fields=''){
        $log_model = D('PumpWaterRecord');
        $station_model = D('PumpingStation');
        $temp = array();
        foreach($pids as $pid){
            $station = $station_model->queryObj($pid, 'id');
            $map['ps_id'] = array('eq', $pid);
            if(!$fields)
                $list = $this->where($map)->order('we_number asc')->select();
            else
                $list = $this->field($fields)->where($map)->order('we_number asc')->select();
            foreach($list as $pump){
                $pump['station'] = $station['ps_name'];
                $pump['model'] = $this->getModelName();
                $pump['statuss'] = filter_device_status($log_model->get_last_one($pid));
                $temp[] = $pump;
                unset($pump);
            }
        }
        return $temp;
    }

    public function get_json_obj($id){
        $art = array(
            'station' => 'PumpingStation',
            'log' => 'PumpWaterRecord',
            'pid_field' => 'ps_id',
            'sta_name_field' => 'ps_name',
        );
        $station_model = D($art['station']);
        $log_model = D($art['log']);
        $obj = $this->queryObj($id, 'id');
        $station = $station_model->queryObj($obj[$art['pid_field']], 'id');
        $obj['station'] = $station[$art['sta_name_field']];
        return json_encode($obj);
    }

    /**
     * @param $pid
     * 暴露的统一接口
     */
    public function count($pid, $start_time, $end_time){
        $map['pumpid'] = array('eq', $pid);
        $list = $this->where($map)->select();
        $water = 0;
        $ele = 0;
        foreach($list as $v){
            $temp = $this->calculat_record($v, $start_time, $end_time);
            $water += $temp['water'];
            $ele += $temp['ele'];
        }

        return array(
            'water' => round($water,1 ),
            'ele' => round($ele,1 ),
        );
    }

    /**
     * @param $pid
     * 计算记录所需要的值
     */
    private function calculat_record($pump, $start_time, $end_time){
        $record_model = D('PumpWaterRecord');
        $map['eleid'] = array('eq', $pump['id']);
        $str = ' date>=\''.$start_time.'\' and date<=\''.$end_time.'\'';
        $list = array_reverse($record_model->query_array_str($map, $str));
        $one = $list[0];
        $last = $list[count($list)];
        $return_array = array();
        $return_array['water'] = abs($last['pump_waterdegrees'] - $one['pump_waterdegrees']);
        $return_array['ele'] = abs($last['pump_pfa'] - $one['pump_pfa']);
        return $return_array;
    }

    public function deleteArray($ids=array()){
        $return = '';
        if(!is_array($ids)){
            $ids = array_filter(explode(',', $ids));
        }
        $fys_models = array(
            'PumpWaterRecord',
        );
        foreach($ids as $id){
            foreach($fys_models as $fy_model){
                $map['eleid'] = array('eq', $id);
                D($fy_model)->where($map)->delete();
            }
            $return = $this->delete($id);
        }
        return $return;
    }
}